package com.hyc.Client.netty.handler;


import com.hyc.Message.testResponse;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.AttributeKey;


/**
 * @author 冷环渊
 * @date 2025/3/22 16:06
 * @description simpleChennlInitlaizer
 */
public class NettyClientHandler extends SimpleChannelInboundHandler<testResponse> {


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, testResponse testResponse) throws Exception {
//            设置别名
        AttributeKey<testResponse> key = AttributeKey.valueOf("testRequest");
//        混入channel
        Channel ctx = channelHandlerContext.channel();
        ctx.attr(key).set(testResponse);
        ctx.close();
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }
}
